/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.dialogs.autocompletion;

import com.google.inject.Inject;
import listo.utils.guice.ManuallyScoped;
import listo.utils.swing.AutoCompleter;
import org.apache.commons.lang.StringUtils;

@ManuallyScoped("TaskDialogScope")
public class QuickEntryCompleter extends BaseCompleter {

    private final DescCompleter descCompleter;
    private final DueDateCompleter dueDateCompleter;
    private final DueTimeCompleter dueTimeCompleter;
    private final FoldersCompleter foldersCompleter;

    @Inject
    public QuickEntryCompleter(DescCompleter descCompleter, DueDateCompleter dueDateCompleter,
                               DueTimeCompleter dueTimeCompleter, FoldersCompleter foldersCompleter) {
        this.descCompleter = descCompleter;
        this.dueDateCompleter = dueDateCompleter;
        this.dueTimeCompleter = dueTimeCompleter;
        this.foldersCompleter = foldersCompleter;
    }

    // default implementation
    public void provideAutoCompletion(AutoCompleter.Context context) {
        if (context.getEventType() == AutoCompleter.EventType.FocusGained) return;

        String[] words = StringUtils.split(context.getTextComponent().getText()
                .substring(0, context.getTextComponent().getCaretPosition()) + '$');
        String lastWord = words.length > 0 ? StringUtils.removeEnd(words[words.length - 1], "$") : "";

        if (lastWord.length() > 0 && lastWord.charAt(0) == '=') {
            context.setCurrentWord(lastWord.substring(1));
            dueDateCompleter.provideAutoCompletion(context);
        } else if (words.length > 1 && words[words.length - 2].charAt(0) == '=' && lastWord.length() > 0 &&
                Character.isDigit(lastWord.charAt(0))) {
            context.setCurrentWord(lastWord);
            dueTimeCompleter.provideAutoCompletion(context);
        } else if (lastWord.length() > 0 && lastWord.charAt(0) == '@') {
            context.setCurrentWord(lastWord.substring(1));
            foldersCompleter.provideAutoCompletion(context);
        } else {
            descCompleter.provideAutoCompletion(context);
        }
    }

    public boolean isWordSeparator(char c) {
        return c == ' ' || c == '=' || c == '@';
    }

}